//
// Created on 2024/8/24.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef ISSUETEST_COMMON_NODE_H
#define ISSUETEST_COMMON_NODE_H

#include "./arkui_base_node.h"

namespace NativeArkUI {
class ArkUICommonNode : public ArkUIBaseNode {
public:
    ArkUICommonNode(ArkUI_NodeType type) : ArkUIBaseNode(type) {}

    /**
     * 设置宽度
     */
    void SetWidth(float width, bool isPercent = false) {
        ArkUI_NumberValue value[] = {width};
        ArkUI_AttributeItem item = {value, 1};
        arkUINativeNodeApi->setAttribute(nodeHandle, isPercent ? NODE_WIDTH_PERCENT : NODE_WIDTH, &item);
    }
    /**
     * 设置高度
     */
    void SetHeight(float height, bool isPercent = false) {
        ArkUI_NumberValue value[] = {height};
        ArkUI_AttributeItem item = {value, 1};
        arkUINativeNodeApi->setAttribute(nodeHandle, isPercent ? NODE_HEIGHT_PERCENT : NODE_HEIGHT, &item);
    }
    /**
     * 设置背景
     */
    void SetBackgroundImage(ArkUI_AttributeItem src) {
        ArkUI_AttributeItem item = src;
        arkUINativeNodeApi->setAttribute(nodeHandle, NODE_BACKGROUND_IMAGE, &item);
    }
    /**
     * 设置背景颜色
     */
    void SetBackgroundColor(uint32_t color) {
        ArkUI_NumberValue value[] = {{.u32 = color}};
        ArkUI_AttributeItem item = {value, 1};
        arkUINativeNodeApi->setAttribute(nodeHandle, NODE_BACKGROUND_COLOR, &item);
    }
    /**
     * 设置外边距
     */
    void SetMargin(float top, float bottom, float left, float right, bool isPercent = false) {
        ArkUI_NumberValue value[] = {{.f32 = top}, {.f32 = bottom}, {.f32 = left}, {.f32 = right}};
        ArkUI_AttributeItem item = {value, 4};
        arkUINativeNodeApi->setAttribute(nodeHandle, isPercent ? NODE_MARGIN_PERCENT : NODE_MARGIN, &item);
    }
    /**
     * 设置边框宽度
     */
    void SetBorderWidth(float top, float bottom, float left, float right, bool isPercent = false) {
        ArkUI_NumberValue value[] = {{.f32 = top}, {.f32 = bottom}, {.f32 = left}, {.f32 = right}};
        ArkUI_AttributeItem item = {value, 4};
        arkUINativeNodeApi->setAttribute(nodeHandle, isPercent ? NODE_BORDER_WIDTH_PERCENT : NODE_BORDER_WIDTH, &item);
    }
    /**
     * 设置边框角度
     */
    void SetBorderRadius(float leftTop, float rightTop, float leftBottom, float rightBottom, bool isPercent = false) {
        ArkUI_NumberValue value[] = {{.f32 = leftTop}, {.f32 = rightTop}, {.f32 = leftBottom}, {.f32 = rightBottom}};
        ArkUI_AttributeItem item = {value, 4};
        arkUINativeNodeApi->setAttribute(nodeHandle, isPercent ? NODE_BORDER_RADIUS_PERCENT : NODE_BORDER_RADIUS,
                                         &item);
    }
    /**
     * 设置边框颜色
     */
    void SetBorderColor(uint32_t color) {
        ArkUI_NumberValue value[] = {{.u32 = color}};
        ArkUI_AttributeItem item = {value, 1};
        arkUINativeNodeApi->setAttribute(nodeHandle, NODE_BORDER_COLOR, &item);
    }
    /**
     * 设置背景图大小
     */
    void SetBackgroundImageSize(float width, float height) {
        ArkUI_NumberValue value[] = {{.f32 = width}, {.f32 = height}};
        ArkUI_AttributeItem item = {value, 2};
        arkUINativeNodeApi->setAttribute(nodeHandle, NODE_BACKGROUND_IMAGE_SIZE, &item);
    }
    /**
     * 设置背景图在拉伸时可调整大小的图像选项，需设置了imageSize后生效
     */
    void SetBackgroundImageResizable(float left, float top, float right, float bottom) {
        ArkUI_NumberValue value[] = {{.f32 = left}, {.f32 = top}, {.f32 = right}, {.f32 = bottom}};
        ArkUI_AttributeItem item = {value, 4};
        arkUINativeNodeApi->setAttribute(nodeHandle, NODE_BACKGROUND_IMAGE_RESIZABLE_WITH_SLICE, &item);
    }
    /**
     * 设置两种颜色的混合模式
     */
    void SetBlendMode(ArkUI_BlendMode mode, ArkUI_BlendApplyType type) {
        ArkUI_NumberValue value[] = {{.i32 = mode}, {.i32 = type}};
        ArkUI_AttributeItem item = {value, 2};
        arkUINativeNodeApi->setAttribute(nodeHandle, NODE_BLEND_MODE, &item);
    }
    /**
     * 设置渐变色
     */
    void SetLinearGradient(ArkUI_LinearGradientDirection direction, std::vector<unsigned int> colors,
                           std::vector<float> stops, float angle = 180, bool isRepeated = false) {
        ArkUI_ColorStop colorStop = {colors.data(), stops.data(), (int)colors.size()};
        ArkUI_ColorStop *ptr = &colorStop;
        ArkUI_NumberValue value[] = {{.f32 = angle}, {.i32 = direction}, {.i32 = isRepeated}};
        ArkUI_AttributeItem item = {value, (sizeof(value) / sizeof(ArkUI_NumberValue)), nullptr,
                                    reinterpret_cast<void *>(ptr)};
        arkUINativeNodeApi->setAttribute(nodeHandle, NODE_LINEAR_GRADIENT, &item);
    }

protected:
    // 组件树操作的实现类对接。
    void OnAddChild(const std::shared_ptr<ArkUIBaseNode> &child) override {
        arkUINativeNodeApi->addChild(nodeHandle, child->GetNodeHandle());
    }
    void OnRemoveChild(const std::shared_ptr<ArkUIBaseNode> &child) override {
        arkUINativeNodeApi->removeChild(nodeHandle, child->GetNodeHandle());
    }
    void OnInsertChild(const std::shared_ptr<ArkUIBaseNode> &child, int32_t index) override {
        arkUINativeNodeApi->insertChildAt(nodeHandle, child->GetNodeHandle(), index);
    }
    void OnClickListener(const std::function<void()> &onClick) override {
        arkUINativeNodeApi->registerNodeEvent(nodeHandle, NODE_ON_CLICK, 0, nullptr);
    }
    void OnTouchListener(const std::function<void(int32_t type, float x, float y)> &onTouch) override {
        arkUINativeNodeApi->registerNodeEvent(nodeHandle, NODE_TOUCH_EVENT, 0, nullptr);
    }
    void OnDisappearCallback(const std::function<void()> &onDisappear) override {
        arkUINativeNodeApi->registerNodeEvent(nodeHandle, NODE_EVENT_ON_DISAPPEAR, 0, nullptr);
    }
    void OnAppearCallback(const std::function<void()> &onAppear) override {
        arkUINativeNodeApi->registerNodeEvent(nodeHandle, NODE_EVENT_ON_APPEAR, 0, nullptr);
    }
    void OnMeasureCallback(const std::function<void(ArkUI_DrawContext *drawContext)> &onAppear) override {
        arkUINativeNodeApi->registerNodeCustomEvent(nodeHandle, ARKUI_NODE_CUSTOM_EVENT_ON_MEASURE, 0, nullptr);
    }
    void OnLayoutCallback(const std::function<void(ArkUI_DrawContext *drawContext)> &onAppear) override {
        arkUINativeNodeApi->registerNodeCustomEvent(nodeHandle, ARKUI_NODE_CUSTOM_EVENT_ON_LAYOUT, 0, nullptr);
    }
    void OnDrawCallback(const std::function<void(ArkUI_DrawContext *drawContext)> &onAppear) override {
        arkUINativeNodeApi->registerNodeCustomEvent(nodeHandle, ARKUI_NODE_CUSTOM_EVENT_ON_DRAW, 0, nullptr);
    }
    void OnForegroundCallback(const std::function<void(ArkUI_DrawContext *drawContext)> &onAppear) override {
        arkUINativeNodeApi->registerNodeCustomEvent(nodeHandle, ARKUI_NODE_CUSTOM_EVENT_ON_FOREGROUND_DRAW, 0, nullptr);
    }
    void OnOverlayDrawCallback(const std::function<void(ArkUI_DrawContext *drawContext)> &onAppear) override {
        arkUINativeNodeApi->registerNodeCustomEvent(nodeHandle, ARKUI_NODE_CUSTOM_EVENT_ON_OVERLAY_DRAW, 0, nullptr);
    }
};
} // namespace NativeArkUI


#endif // ISSUETEST_COMMON_NODE_H
